package com.moorror.codegenvel.db.sqlite.dao.impl;

import com.moorror.codegenvel.db.sqlite.dao.BaseDAO;
import com.moorror.codegenvel.db.sqlite.dao.CodeGenTemplateDao;
import com.moorror.codegenvel.db.sqlite.entity.CodeGenTemplate;
import com.moorror.codegenvel.utils.DateUtils;
import com.moorror.codegenvel.utils.SqliteUtils;
import org.apache.commons.collections4.CollectionUtils;

import java.sql.Connection;
import java.util.List;

/**
 * @Author jiyanghuang
 * @Date 2025/8/21 07:57
 */
public class CodeGenTemplateDaoImpl extends BaseDAO<CodeGenTemplate> implements CodeGenTemplateDao<CodeGenTemplate> {

    private static CodeGenTemplateDaoImpl instance;

    private CodeGenTemplateDaoImpl() {

    }

    public static synchronized CodeGenTemplateDaoImpl getInstance() {
        if (null == instance) {
            instance = new CodeGenTemplateDaoImpl();
        }
        return instance;
    }

    private static final String SELECT_SQL = " select id,name,desc_template,content,json_context,create_time,update_time from codegen_template ";

    @Override
    public String tableName() {
        return "codegen_template";
    }

    @Override
    public void insert(Connection conn, CodeGenTemplate[] ary) {
        if (ary == null || ary.length == 0) {
            return;
        }
        //数据库中存在自增列后，SQLite 就会创建一个 sqlite_sequence 表。所有表的自增列都共享这个表，sqlite_sequence 分别维护着每个自增列的当前值，所以自增列的计数也是单独的。
        String sql = "insert into codegen_template(name,desc_template,content,json_context,create_time,update_time) values(?,?,?,?,?,?)";
        int size = ary.length;
        if (size == 1) {
            CodeGenTemplate codeGenTemplate = ary[0];
            update(conn, sql,  codeGenTemplate.getName(), codeGenTemplate.getDescTemplate(), codeGenTemplate.getContent(), codeGenTemplate.getJsonContext(), codeGenTemplate.getCreateTime(), codeGenTemplate.getUpdateTime());
        } else {
            Object[][] args = new Object[size][5];
            for (int i = 0; i < size; i++) {
                CodeGenTemplate codeGenTemplate = ary[i];
                args[i][0] = codeGenTemplate.getName();
                args[i][1] = codeGenTemplate.getDescTemplate();
                args[i][2] = codeGenTemplate.getContent();
                args[i][3] = codeGenTemplate.getJsonContext();
                args[i][4] = codeGenTemplate.getCreateTime();
                args[i][5] = codeGenTemplate.getUpdateTime();
            }
            updateBatch(conn, sql, args);
        }
    }

    @Override
    public CodeGenTemplate insert(Connection conn, CodeGenTemplate codeGenTemplate) {
        String sql = "insert into codegen_template(name,desc_template, content,json_context,create_time,update_time) values(?,?,?,?,?,?)";
        update(conn, sql, codeGenTemplate.getName(), codeGenTemplate.getDescTemplate(), codeGenTemplate.getContent(), codeGenTemplate.getJsonContext(), codeGenTemplate.getCreateTime(), codeGenTemplate.getUpdateTime());
        String sqlSelect = SELECT_SQL + " where id = (select LAST_INSERT_ROWID()) limit 1";
        return getBean(conn, sqlSelect);
    }

    @Override
    public void delete(Connection conn, Integer id) {

    }

    @Override
    public boolean deleteByUnique(Connection conn, CodeGenTemplate updateT) {
        String sql = "delete from codegen_template where name = ? ";
        return update(conn, sql, updateT.getName()) > 0;
    }

    @Override
    public void update(Connection conn, CodeGenTemplate[] ary) {

    }

    @Override
    public void update(Connection conn, CodeGenTemplate codeGenTemplate) {

    }

    @Override
    public boolean updateByUnique(Connection conn, CodeGenTemplate updateT) {
        String sql = "update codegen_template SET desc_template = ?, content = ? , json_context = ?, update_time = ? where name = ? ";
        return update(conn, sql, updateT.getDescTemplate(), updateT.getContent(), updateT.getJsonContext(), DateUtils.currentTime(), updateT.getName()) > 0;
    }

    @Override
    public CodeGenTemplate findById(Connection conn, Integer id) {
        return null;
    }

    @Override
    public CodeGenTemplate findByUnique(Connection conn, CodeGenTemplate queryT) {
        String sql = SELECT_SQL + " where name = ? ";
        List<CodeGenTemplate> re = queryList(conn, sql, queryT.getName());
        if (CollectionUtils.isEmpty(re)) {
            return null;
        }
        return re.get(0);
    }

    @Override
    public List<CodeGenTemplate> queryIn(Connection conn, List<String> keys) {
        String inSql = SqliteUtils.inSql(keys);
        String sql = SELECT_SQL + " where name IN " + inSql;
        return queryList(conn, sql, keys.toArray(new Object[0]));
    }

    @Override
    public void exchangeShowOrder(Connection conn, Integer showOrder1, Integer showOrder2) {

    }
}
